<?php
namespace App\Service\Admin;

class TongjiService1001 extends BaseService
{
    //统计

    //个人
    public static function person($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWhItem)->where('del', 0);
        if ($pas['uid']) {
            $db = $db->where('uid', $pas['uid']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            //->orderBy('id', 'desc')
            ->orderBy('starthour', 'asc')
            ->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $item_title = '?';
            $wh_title = '?';
            $configOne = self::dbConn(self::$dbConfigWorkHour)->where('id', $v->config_whid)->first();
            if ($configOne) {
                $wh_title = $configOne->title;
            }
            $whOne = self::dbConn(self::$dbYwWh)->where('id', $v->wh_id)->first();
            if ($whOne && $whOne->item_id) {
                $itemOne = self::dbConn(self::$dbConfigItem)->where('id', $whOne->item_id)->first();
                if (!empty($itemOne)) $item_title = $itemOne->title;
            }
            $resDelay = self::getDelay($v->starthour, $v->endhour);
            $delay_str = $resDelay['code']==0 ? $resDelay['data']['str'] : '';
            $whOne = (array) self::dbConn(self::$dbYwWh)->where('id', $v->wh_id)->first();
            $in_day = empty($whOne) ? '-' : $whOne['in_day'];
            $arys[] = [
                'id' => $v->id, 'item_title' => $item_title,
                'wh_title' => $wh_title, 'title' => $v->title,
                'starthour' => $v->starthour, 'endhour' => $v->endhour,
                'delay_str' => $delay_str,
                'in_day' => $in_day,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //部门
    public static function item($pages, $pas)
    {
        $leadOne = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_lead', 1)
            ->first();
        if (empty($leadOne)) {
            return self::toback(-2, '没有权限');
        }
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWhItem)
            ->where('del', 0)
            ->where('depart_id', $pas['depart_id']);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $staffOne = self::dbConn(self::$dbStaff)
                ->where('id', $v->uid)
                ->first();
            $staff_title = empty($staffOne) ? '?' : $staffOne->title;
            $item_title = '？';
            $wh_title = '？';
            $whOne = self::dbConn(self::$dbYwWh)->where('id', $v->wh_id)->first();
            if ($whOne && $whOne->item_id) {
                $itemOne = self::dbConn(self::$dbConfigItem)->where('id', $whOne->item_id)->first();
                if (!empty($itemOne)) $item_title = $itemOne->title;
            }
            $configOne = self::dbConn(self::$dbConfigWorkHour)->where('id', $v->config_whid)->first();
            if ($configOne) {
                $wh_title = $configOne->title;
            }
            $delay = 0;
            $itemOnes = self::dbConn(self::$dbYwWhItem)
                ->where('wh_id', $v->id)
                ->get();
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $delay += $vv->delay;
                }
            }
            $arys[] = [
                'id' => $v->id, 'item_title' => $item_title,
                'wh_title' => $wh_title, 'title' => $v->title,
                'delay' => $delay, 'staff_title' => $staff_title,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //审核员
    public static function all($pages, $pas)
    {
        if ($pas['role_id'] != 5) {
            return self::toback(-2, '只有审核员有权限');
        }
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWhItem)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $staffOne = self::dbConn(self::$dbStaff)
                ->where('is_able', 0)
                ->where('depart_lead', 1)
                ->first();
            $staff_title = empty($staffOne) ? '?' : $staffOne->title;
            $item_title = '？';
            $wh_title = '？';
            $whOne = self::dbConn(self::$dbYwWh)->where('id', $v->wh_id)->first();
            if ($whOne && $whOne->item_id) {
                $itemOne = self::dbConn(self::$dbConfigItem)->where('id', $whOne->item_id)->first();
                if (!empty($itemOne)) $item_title = $itemOne->title;
            }
            $configOne = self::dbConn(self::$dbConfigWorkHour)->where('id', $v->config_whid)->first();
            if ($configOne) {
                $wh_title = $configOne->title;
            }
            $arys[] = [
                'id' => $v->id, 'staff_title' => $staff_title,
                'item_title' => $item_title, 'wh_title' => $wh_title,
                'title' => $v->title, 'delay' => $v->delay,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //环比
    public static function huan($pages, $pas)
    {
        $leadOne = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_id', $pas['depart_id'])
            ->where('depart_lead', 1)
            ->first();
        if (empty($leadOne)) {
            return self::toback(-2, '没有权限');
        }
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_id', $pas['depart_id'])
            ->where('depart_lead', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $delay = self::dbConn(self::$dbYwWhItem)->where('uid', $v->id)->sum('delay');
            //同比，一年内
            $year0 = date('Y-m-d H:i:s', strtotime("-1 year"));
            $year1 = date('Y-m-d H:i:s', strtotime("-2 year"));
            $delay_year0 = self::dbConn(self::$dbYwWhItem)
                ->where('uid', $v->id)
                ->where('addtime', '>=', $year0)
                ->where('addtime', '<=', toDate())
                ->sum('delay');
            $delay_year1 = self::dbConn(self::$dbYwWhItem)
                ->where('uid', $v->id)
                ->where('addtime', '>=', $year1)
                ->where('addtime', '<=', $year0)
                ->sum('delay');
            $year = $delay_year1 ? floor($delay_year0 / $delay_year1) : 0;
            //环比，一周内
            $week0 = date('Y-m-d H:i:s', strtotime("-1 week"));
            $week1 = date('Y-m-d H:i:s', strtotime("-2 week"));
            $delay_week0 = self::dbConn(self::$dbYwWhItem)
                ->where('uid', $v->id)
                ->where('addtime', '>=', $week0)
                ->where('addtime', '<=', toDate())
                ->sum('delay');
            $delay_week1 = self::dbConn(self::$dbYwWhItem)
                ->where('uid', $v->id)
                ->where('addtime', '>=', $week1)
                ->where('addtime', '<=', $week0)
                ->sum('delay');
            $week = $delay_week1 ? floor($delay_week0 / $delay_week1) : 0;
            $arys[] = [
                'id' => $v->id, 'staff_title' => $v->title,
                'delay' => $delay, 'year' => $year, 'week' => $week,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //图
    public static function tu()
    {
        $leadOne = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_lead', 1)
            ->first();
        if (empty($leadOne)) {
            return self::toback(-2, '没有权限');
        }
        $arys = [
            'bing' => [['xxx', 10], ['zzz', 20]], //项目占比
            'zhu' => [ //工时占比
                'title' => ['张三', '李四'],
                'val' => [[10], [50]],
            ],
        ];
//        return self::toback(0, '', $arys);
        $itemOnes = self::dbConn(self::$dbConfigItem)
            ->where('del', 0)
            ->get();
        if (count($itemOnes)) {
            foreach ($itemOnes as $v) {
                $delay = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    ->where('item_id', 0)
                    ->sum('delay');
                $arys['bing'][] = [
                    $v->title, $delay,
                ];
            }
        }
        $staffOnes = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_lead', 0)
            ->get();
        if (count($staffOnes)) {
            foreach ($staffOnes as $v) {
                $delay = self::dbConn(self::$dbYwWhItem)
                    ->where('del', 0)
                    ->where('uid', 0)
                    ->sum('delay');
                $arys['zhu']['title'][] = $v->title;
                $arys['zhu']['val'][] = [$delay];
            }
        }
        return self::toback(0, '', $arys);
    }

    //选项
    public static function getOption()
    {
        $arys = ['staff' => []];
        $arys['staff'] = self::dbConn(self::$dbStaff)
            ->select('id', 'title')
            ->where('is_able', 0)
            ->get();
        $arys['depart'] = self::dbConn(self::$dbStDepart)
            ->select('id', 'title')
            ->where('title', '<>', '')
            ->where('del', 0)
            ->get();
        return self::toback(0, '', $arys);
    }

//    //获取员工月底数据
//    public static function staffMonthList($staff_id,  $year, $month)
//    {
//        $staffOne = (array) self::dbConn(self::$dbStaff)
//            ->where('is_able', 0)
//            ->where('id', $staff_id)
//            ->first();
//        if (empty($staffOne)) {
//            return self::toback(-2, '没有该员工');
//        }
//        $depart_id = $staffOne['depart_id'];
//        $departOne = (array) self::dbConn(self::$dbStDepart)
//            ->where('id', $depart_id)
//            ->first();
//        $depart_name = $departOne['title'];
//        $configWhOnes = self::dbConn(self::$dbConfigWorkHour)
//            ->where('del', 0)
//            ->where('depart_id', $depart_id)
//            ->get();
//        if (!count($configWhOnes)) {
//            return self::toback(-2, '没有工时配置');
//        }
//        //标题数据
//        $title[] = '员工';
//        $title[] = '部门';
//        $title[] = '工时分配';
//        $month = strlen($month)==1 ? '0'.$month : $month;
//        $days = [];
//        $heji = [];
//        $day_num = date('t', strtotime($year.'-'.$month.'-01'));
//        for ($i = 1; $i <= $day_num; $i ++) {
//            $title[] = $i;
//            $days[] = $i;
//            $heji[$i] = 0;
//        }
//        $title[] = '合计';
//        $arys['data'][] = $title;
//        $heji_total = 0;
//        foreach ($configWhOnes as $v) {
//            $tlist = [$staffOne['title'], $depart_name, $v->title];
//            $month_total = 0;
//            foreach ($days as $day) {
//                $day_str = strlen($day)==1 ? '0'.$day : $day;
//                $daystr = $year.'-'.$month.'-'.$day_str;
//                $daystart = $daystr.' 00:00:01';
//                $dayend = $daystr.' 23:59:59';
//                $delay = self::dbConn(self::$dbYwWhItem)
//                    ->where('del', 0)
//                    ->where('uid', $staff_id)
//                    ->where('config_whid', $v->id)
//                    ->where('uptime', '>=', $daystart)
//                    ->where('uptime', '<=', $dayend)
//                    ->sum('delay');
//                $total = number_format($delay / 60, 1);
//                $heji[$day] += $delay;
//                $heji_total += $delay;
//                $tlist[] = $total;
//                $month_total += $total;
//            }
//            $tlist[] = $month_total;
//            $arys['data'][] = $tlist;;
//        }
//        $uid_heji = [
//            $staffOne['title'], $depart_name, '合计',
//        ];
//        foreach ($heji as $v) {
//            $uid_heji[] = number_format($v / 60, 1);
//        }
//        $uid_heji[] = number_format($heji_total / 60, 1);
//        $arys['data'][] = $uid_heji;
//        $arys['staff_name'] = $staffOne['title'];
//        return self::toback(0, '', $arys);
//    }

//    //获取员工月底数据
//    public static function staffMonthList($staff_id,  $year, $month)
//    {
//        $staffOne = (array) self::dbConn(self::$dbStaff)
//            ->where('is_able', 0)
//            ->where('id', $staff_id)
//            ->first();
//        if (empty($staffOne)) {
//            return self::toback(-2, '没有该员工');
//        }
//        $depart_id = $staffOne['depart_id'];
//        $itemOnes = self::dbConn(self::$dbYwWhItem)
//            ->where('del', 0)
//            ->where('depart_id', $depart_id)
//            ->groupBy('config_whid')
//            ->get();
//        if (!count($itemOnes)) {
//            return self::toback(-2, '没有数据');
//        }
//        //标题数据
//        $title[] = '员工';
//        $title[] = '项目名称';
//        $title[] = '工时分配';
//        $month = strlen($month)==1 ? '0'.$month : $month;
//        $days = [];
//        $heji = [];
//        $day_num = date('t', strtotime($year.'-'.$month.'-01'));
//        for ($i = 1; $i <= $day_num; $i ++) {
//            $title[] = $i;
//            $days[] = $i;
//            $heji[$i] = 0;
//        }
//        $title[] = '合计';
//        $heji_total = 0;
//        $item_name = '';
//        $arys['data'][] = $title;
//        foreach ($itemOnes as $v) {
//            $whOne = (array) self::dbConn(self::$dbYwWh)
//                ->where('id', $v->wh_id)
//                ->first();
//            $item_id = empty($whOne) ? '-' : $whOne['item_id'];
//            $itemOne = (array) self::dbConn(self::$dbConfigItem)
//                ->where('id', $item_id)
//                ->first();
//            $item_name = empty($itemOne) ? '-' : $itemOne['title'];
//            $configOne = (array) self::dbConn(self::$dbConfigWorkHour)
//                ->where('id', $v->config_whid)
//                ->first();
//            $config_name = empty($configOne) ? '-' : $configOne['title'];
//            $tlist = [$staffOne['title'], $item_name, $config_name];
//            $month_total = 0;
//            foreach ($days as $day) {
//                $day_str = strlen($day)==1 ? '0'.$day : $day;
//                $daystr = $year.'-'.$month.'-'.$day_str;
//                $daystart = $daystr.' 00:00:01';
//                $dayend = $daystr.' 23:59:59';
//                $delay = self::dbConn(self::$dbYwWhItem)
//                    ->where('del', 0)
//                    ->where('uid', $staff_id)
//                    ->where('config_whid', $v->config_whid)
//                    ->where('uptime', '>=', $daystart)
//                    ->where('uptime', '<=', $dayend)
//                    ->sum('delay');
//                $total = number_format($delay / 60, 1);
//                $heji[$day] += $delay;
//                $heji_total += $delay;
//                $tlist[] = $total;
//                $month_total += $total;
//            }
//            $tlist[] = $month_total;
//            $arys['data'][] = $tlist;;
//        }
//        $uid_heji = [
//            $staffOne['title'], $item_name, '合计',
//        ];
//        foreach ($heji as $v) {
//            $uid_heji[] = number_format($v / 60, 1);
//        }
//        $uid_heji[] = number_format($heji_total / 60, 1);
//        $arys['data'][] = $uid_heji;
//        $arys['staff_name'] = $staffOne['title'];
//        return self::toback(0, '', $arys);
//    }

    //获取员工月底数据
    public static function staffMonthList($depart_id, $staff_id,  $year, $month)
    {
        $departOne = (array) self::dbConn(self::$dbStDepart)
            ->where('del', 0)
            ->where('id', $depart_id)
            ->first();
        if (empty($departOne)) {
            return self::toback(-2, '部门必须的');
        }
        //标题数据
        $arys = [];
        $header = ['部门名称：'.$departOne['title'], '', '', '月份：'.$year.'年'.$month.'月'];
        for ($i = 1; $i < 30; $i ++) {
            $header[] = '';
        }
        $arys['data'][] = $header;
        $titles[] = '员工姓名';
        $titles[] = '项目名称';
        $titles[] = '工时分配';
        $month = strlen($month)==1 ? '0'.$month : $month;
        $days = [];
        $day_num = date('t', strtotime($year.'-'.$month.'-01'));
        for ($i = 1; $i <= $day_num; $i ++) {
            $titles[] = $i;
            $days[] = $i;
            $heji[$i] = 0;
        }
        $titles[] = '合计';
        $arys['data'][] = $titles;
        $staffOnes = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_id', $depart_id)
            ->get();
        if (count($staffOnes)) {
            foreach ($staffOnes as $staffOne) {
                $uid = $staffOne->id;
                $uname = $staffOne->title;
                $heji = [];
                $whOnes = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    ->where('staff_id', $uid)
                    ->groupBy('item_id')
                    ->get();
                if (count($whOnes)) {
                    $is_empty = 0;
                    foreach ($whOnes as $whOne) {
                        $cfgItemOne = (array) self::dbConn(self::$dbConfigItem)
                            ->where('id', $whOne->item_id)
                            ->first();
                        $item_name = empty($cfgItemOne) ? '' : $cfgItemOne['title'];
                        $heji_total = 0;
                        $cfgOnes = self::dbConn(self::$dbYwWhItem)
                            ->where('del', 0)
                            ->where('wh_id', $whOne->id)
                            ->groupBy('config_whid')
                            ->get();
                        if (count($cfgOnes)) {
                            foreach ($cfgOnes as $cfgOne) {
                                $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                                    ->where('id', $cfgOne->config_whid)
                                    ->first();
                                $config_name = empty($cfgWhOne) ? '' : $cfgWhOne['title'];
                                $tlist = [$uname, $item_name, $config_name];
                                $month_total = 0;
                                foreach ($days as $day) {
                                    $heji[$day] = isset($heji[$day]) ? $heji[$day] : 0;
                                    $day_str = strlen($day)==1 ? '0'.$day : $day;
                                    $daystr = $year.'-'.$month.'-'.$day_str;
                                    $daystart = $daystr.' 00:00:01';
                                    $dayend = $daystr.' 23:59:59';
                                    $delay = self::dbConn(self::$dbYwWhItem)
                                        ->where('del', 0)
                                        ->where('uid', $uid)
                                        ->where('config_whid', $cfgOne->config_whid)
                                        ->where('uptime', '>=', $daystart)
                                        ->where('uptime', '<=', $dayend)
                                        ->sum('delay');
                                    $total = number_format($delay / 60, 1);
                                    $heji[$day] += $delay;
                                    $heji_total += $delay;
                                    $tlist[] = $total;
                                    $month_total += $total;
                                    $is_empty = 1;
                                }
                                $tlist[] = $month_total;
                                $arys['data'][] = $tlist;
                            }
                        }
                    }
                    if ($is_empty) {
                        $u_heji = [$uname, $item_name, '合计'];
                        $u_delay = 0;
                        foreach ($days as $day) {
                            $day_heji = $heji[$day];
                            $day_heji_delay = number_format($day_heji / 60, 1);
                            $u_heji[] = $day_heji_delay;
                            $u_delay += $day_heji;
                        }
                        $u_total = number_format($u_delay / 60, 1);
                        $u_heji[] = $u_total;
                        //ddd($days, $heji, $u_heji);
                        $arys['data'][] = $u_heji;
                     }
                }
            }
        }
        $arys['depart_name'] = $departOne['title'];
        return self::toback(0, '', $arys);
    }
}